// pages/order/index.js
var app = getApp(),
  a = app.requirejs("core");
Page( { 
 data: {   
  currentTab: 0,
  scrollTop:0,
  scrollLeft:0,
  page:1,
  ordersList:[],
  loading:true
 }, 
 onLoad: function(options) {
  var current = options.id;
  this.data.currentTab = current?current:0
  this.systemInfo = wx.getSystemInfoSync()
    this.setData({
      currentTab:this.data.currentTab,
      windowHeight:this.systemInfo.windowHeight
    })
 },
 onShow:function(options){
  if(this.data.currentTab==0){
    this.setCurrentData()
  }
 },
 setCurrentData:function(){
   var t = this;
   t.setData({
     loading: true
   }),
     a.get("Pintuan/ptOrderList", {
       page: t.data.page,
     status: t.data.currentTab,
       uid: getApp().getCache("userinfo").uid
     }, function (e) {
       console.log(e)
       0 == e.code ? (t.setData({
         loading: false,
         show: true
       }),
         e.info.length > 0 && t.setData({
           page: t.data.page + 1,
         ordersList: t.data.ordersList.concat(e.info)
         }),
         e.info.length < 10 && t.setData({
           loaded: true
         })) : a.alert(e.msg)
     }, true)
 },
 toGroupDetail:function(e){
  var id = e.currentTarget.dataset.id;
  app.redirect('group/detail','id='+id)
 },
 toPay:function(e){
    wx.showLoading({
      title: '正在提交',
      mask : true,
    })
  var self = this
  var data = e.currentTarget.dataset
   a.get('Pintuan/ptPay', {
     oid:data.id,
     openid: getApp().getCache("userinfo").openid
   }, function (t) {
     wx.hideLoading();
     if (t.code == 0) {
       wx.requestPayment({
         'timeStamp': t.info.timeStamp,
         'nonceStr': t.info.nonceStr,
         'package': t.info.package,
         'signType': 'MD5',
         'paySign': t.info.paySign,
         'success': function (res) {
           console.log(res)
           if (data.isGroup == 1) { //拼团
             // 重定向到团详情页面
             wx.redirectTo({
               url: '/yb_shop/pages/pintuan/pages/group/detail?id=' + data.id,
             })
             // app.redirect('group/detail','id='+oid)
           } else {
             // 重定向到订单页面
             self.setData({
               ordersList: [],
               page: 1,
               loaded: false
             })
             self.setCurrentData()
             // app.redirect('orders/index','id=3')
           }
         },
         'fail': function (res) {
          // a.alert('您已取消了支付')
         }
       })
     } else {
       a.alert(t.msg, function () {
         wx.redirectTo({
           url: '/yb_shop/pages/pintuan/pages/orders/index',
         })
       });
     }

   })
 },
 confirmReceipt:function(e){
  var self = this;
  var id = e.currentTarget.dataset.id;
  wx.showModal({
    content: '是否确认收货？',
    success: function(res) {
      if (res.confirm) {
        a.get('Pintuan/SignOrder',{
          id:id,
          uid:getApp().getCache('userinfo').uid
        },function(t){
            if(t.code==0){
              a.success('收货成功');
              setTimeout(function(){
                self.setData({
                  ordersList: [],
                  page: 1,
                  loaded: false
                })
                self.setCurrentData()
              })
            }else{
              a.alert(t.msg)
            }
     })
      }
    }
  })
 },
  tuikuan: function (e) {
    var self = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      content: '是否要申请退款？',
      success: function (res) {
        if (res.confirm) {
          a.get('Pintuan/refundOrder', {
            id: id,
            uid: getApp().getCache('userinfo').uid
          }, function (t) {
            if (t.code == 0) {
              a.success('申请成功');
              setTimeout(function () {
                self.setData({
                  ordersList: [],
                  page: 1,
                  loaded: false
                })
                self.setCurrentData()
              })
            } else {
              a.alert(t.msg)
            }
          })
        }
      }
    })
  },
 showOrderDetail:function(e){
  var id = e.currentTarget.dataset.id;
  app.redirect('orders/detail','oid='+id)
 },
 showGoodsDetial:function(e){
  var id = e.currentTarget.dataset.id;
  app.redirect('goods/detail','gid='+id);
 },
 // 滑动切换tab 
 bindChange: function( e ) { 
  this.data.page = 0
  this.data.ordersList=[]
  this.data.loading =true
  this.data.currentTab = e.detail.current
  this.setCurrentData()
  this.setData({
    loading:true,
    ordersList:[],
    currentTab: this.data.currentTab
  })
 }, 
 // 点击tab切换 
 swichNav: function( e ) {
    if( this.data.currentTab == e.currentTarget.dataset.current ) return;

    this.data.currentTab = e.currentTarget.dataset.current
    var windowWidth = this.systemInfo.windowWidth
    var offsetLeft = e.currentTarget.offsetLeft
    var scrollLeft = this.data.scrollLeft;
    if (offsetLeft > windowWidth / 2) {
      scrollLeft = offsetLeft
    } else {
      scrollLeft = 0
    }
    this.setData({
      scrollLeft: scrollLeft,
      currentTab: this.data.currentTab
    })
 },
  /**
    * 下拉刷新
    */
  onPullDownRefresh: function () {
    this.setData({
      ordersList: [],
      page: 1,
      loaded: false
    })
    this.setCurrentData()
    wx.stopPullDownRefresh();
  },
 scrolltolower:function(){
   console.log('加载更多')
   this.data.loaded || this.setCurrentData()
 }
})